package com.qyoung.controller;

import com.alibaba.fastjson.JSON;
import com.qyoung.dao.UserDao;
import com.qyoung.dao.impl.UserDaoImpl;
import com.qyoung.pojo.User;

import javax.json.Json;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/testJSONServlet")
public class testJSONServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        resp.setContentType("text/html;charset=utf-8");
        int id = Integer.parseInt(userId);
        UserDao dao = new UserDaoImpl();
        User user = dao.findById(id);
        Object json = JSON.toJSON(user);
        PrintWriter writer = resp.getWriter();
        writer.print(json);
    }
}
